package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.admin.util.PageUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LumierePayMemberUserLogVo;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.PayMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 管理支付会员用户日志服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Service
public class AdminPayMemberUserLogService {

    /**
     * 付费会员服务
     */
    @Autowired
    private PayMemberService payMemberService;

    @Autowired
    private LitemallOrderService litemallOrderService;

    /**
     * 查询支付会员用户日志
     *
     * @param userName     用户名
     * @param memberName   成员名
     * @param purchaseType 购买类型
     * @param buyTime      争取时间
     * @param payType      支付类型
     * @param page         页面
     * @param limit        限制
     * @param sort         排序
     * @param order        订单
     * @return {@link Object}
     */
    public Object queryPayMemberUserLog(String userName, String memberName,
                                        Byte purchaseType,String buyTime,Byte payType,Integer page,Integer limit,
                                        String sort,String order){
        LocalDate date = null;
        if(buyTime != null){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            date = LocalDate.parse(buyTime, fmt);
        }
        List<LumierePayMemberUserLogVo> vos = payMemberService.queryPayMemberUserLogs(userName,memberName,purchaseType,date,payType,sort,order);
        Pageable pageable = PageRequest.of(page - 1,limit);
        PageImpl<LumierePayMemberUserLogVo> list = PageUtil.getPageLumierePayMemberUserLogVo(pageable,vos);
        return ResponseUtil.ok(list);
    }


    /**
     * 查询支付会员用户日志uid
     *
     * @param userId       用户id
     * @param page         页面
     * @param limit        限制
     * @param sort         排序
     * @param order        订单
     * @return {@link ResponseUtil}
     */
    public ResponseUtil.Response queryPayMemberUserLogByUid(String userId, Integer page, Integer limit, String sort, String order) {
        List<LumierePayMemberUserLogVo> vos = payMemberService.queryPayMemberUserLogsByUid(userId,sort,order);
        Pageable pageable = PageRequest.of(page - 1,limit);
        PageImpl<LumierePayMemberUserLogVo> list = PageUtil.getPageLumierePayMemberUserLogVo(pageable,vos);
        /*list.getContent().stream().forEach(payMemberUserLogVo -> {
            LitemallOrder litemallOrder = litemallOrderService.getOne(Wrappers.lambdaQuery(LitemallOrder.class)
                    .eq(LitemallOrder::getUserId, payMemberUserLogVo.getUserId())
                    .eq(LitemallOrder::getPayMemberId, payMemberUserLogVo.getMemberId())
            );
            payMemberUserLogVo.setOrderSn(ObjectUtil.isNotEmpty(litemallOrder)?litemallOrder.getOrderSn():"");
        });*/
        return ResponseUtil.ok(list);
    }
}
